<?php
class CouponsController extends Sosweb_Controller_Action
{
	function init()
	{
		parent::init();
		$this->logger->info('Inicjalizacja strony z kuponami profitmania.pl przez uzytkownika z IP: '.$_SERVER['REMOTE_ADDR']);
		
		$this->_helper->layout()->getView()->headTitle('ProfitMania.pl - Zarabiaj na zakupach - '.Sosweb_Const_DescriptionConst::$TITLE_1, 'SET');
		$this->_helper->layout()->getView()->getHelper('HeadMeta')->appendName('description', Sosweb_Const_DescriptionConst::$DESC_1);		
	}

	function indexAction()
	{
		$request = clone $this->getRequest();

		//po jakiej kolumnie sortujemy
		$sortColumn = $request->getParam('sort');
		//jaka kategorie chcemy ogladac
		$category = $request->getParam('category');		
		//numer porcji danych
		$pageNo = $request->getParam('pageNo', 1);
		//identyfikator kuponu
		$id = $request->getParam('id');
		
	    try
       	{
       		$dataPortion = Sosweb_Const_ProfitmaniaConst::$TABLE_VIEW_DATA_NUM;
		
			$current_date = date('Y-m-d'); 
			$where = $this->createWhereForCategory($category);
			
			//spreparowanie fragmentu zapytania odpowiedzialnego za sortowanie
			if ($sortColumn != null)
			{
				$sortColumnText = $sortColumn;
				$descending_columns = array('posted_date', 'views');
				$is_desc = in_array($sortColumnText, $descending_columns);
				
				$this->logger->info('Pozycja: '.$is_desc);
				
				if ($is_desc)
				{
					$sortColumnText .= ' DESC';	
				}				
			}
			else
			{
				$sortColumnText = 'id DESC';
			}
			
			$coupons_view = new CouponsView();
	    	$select = $coupons_view->select();
			$select->limit($dataPortion, ($pageNo-1)*$dataPortion)
				->order($sortColumnText);
	
			$select_cnt = $coupons_view->select();
			$select_cnt->setIntegrityCheck(false)
				->from($coupons_view, 'count(*) as data_count');	
	
			if ($id != null)
			{
				$select->where('id = ?', $id);
				$select_cnt->where('id = ?', $id);
			}
			else
			{
				if ($where != null)
				{
					$select->where($where['cond'], $where['value']);
					$select_cnt->where($where['cond'], $where['value']);
				}
			}

			//pobieranie danych
       		$this->logger->info('Fetching coupons data. '.$select->assemble());
       		$this->logger->info('Fetching coupons data count. '.$select_cnt->assemble());
			$rows = $coupons_view->fetchAll($select);
			$row = $coupons_view->fetchRow($select_cnt);
			
			if(count($rows)== 0)
			{
				$this->logger->warn('Nie znaleziono danych o kuponach.');
			}

			$this->view->pageNo = $pageNo;
			$this->view->sortColumn = $sortColumn;
			$this->view->category = $category;
			$this->view->categories = $this->createCategories();
			$this->view->coupons = $rows;
			$this->view->dataCount = $row->data_count;
       	}
		catch(Zend_Exception $ex)
		{
			$this->logger->error($ex->getMessage());
			$this->view->message = 'Wystąpił błąd aplikacji podczas pobierania danych o kuponach.';
			
			return $this->_forward('index', 'index');
		}		
	}
	
	private function createWhereForCategory($category)
	{
		$where = array();
		
		switch ($category)
		{
			case 1 :
				$current_date = date('Y-m-d');
				$where['cond'] = 'posted_date = ?';
				$where['value'] = $current_date;
				break;
			case 2 :
				$current_date = date('Y-m-d');
				$where['cond'] = 'expiry_date = ?';
				$where['value'] = $current_date;
				break;
			case 3 :
				$where['cond'] = 'upper(substring(title, 1, 1)) not in (?)';
				$where['value'] = array('D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'Ł', 'M', 'N', 'O', 'Q', 'P', 'R', 'S', 'Ś', 'T', 'U', 'V', 'W', 'X', 'Z', 'Ż', 'Ź');
				break;
			case 4 :
				$where['cond'] = 'upper(substring(title, 1, 1)) in (?)';
				$where['value'] = array('D', 'E', 'F', 'G');
				break;
			case 5 :
				$where['cond'] = 'upper(substring(title, 1, 1)) in (?)';
				$where['value'] = array('H', 'I', 'J', 'K', 'L', 'Ł');
				break;
			case 6 :
				$where['cond'] = 'upper(substring(title, 1, 1)) in (?)';
				$where['value'] = array('M', 'N', 'O', 'Q', 'P', 'R');				
				break;
			case 7 :
				$where['cond'] = 'upper(substring(title, 1, 1)) in (?)';
				$where['value'] = array('S', 'Ś', 'T', 'U', 'V', 'W', 'X', 'Z', 'Ż', 'Ź');
				break;																				
			default:
				$where = null;				
		}
		
		return $where;
	}
	
	private function createCategories()
	{
		 $categories = array();
		 $categories['0'] = array('text'=>'Wszystkie', 'tooltip'=>'Wszystkie kupony.');
		 $categories['1'] = array('text'=>'Nowe Kupony', 'tooltip'=>'Kupony dodane w dniu dzisiejszym.');
		 $categories['2'] = array('text'=>'Wygasające Kupony', 'tooltip'=>'Kupony wygasające w dniu dzisiejszym.');
		 $categories['3'] = array('text'=>'# & A-C', 'tooltip'=>'Kupony o tytule zaczynającym się na znaki specjalne oraz litery od A do C.');
		 $categories['4'] = array('text'=>'D-G', 'tooltip'=>'Kupony o tytule zaczynającym się na litery od D do G');
		 $categories['5'] = array('text'=>'H-Ł', 'tooltip'=>'Kupony o tytule zaczynającym się na litery od H do Ł');
		 $categories['6'] = array('text'=>'M-R', 'tooltip'=>'Kupony o tytule zaczynającym się na litery od M do R');
		 $categories['7'] = array('text'=>'S-Ź', 'tooltip'=>'Kupony o tytule zaczynającym się na litery od S do Ź');
		
		 return $categories;
	}
}